package com.mrjy.share.bo;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.mrjy.common.entity.DistributorInfo;
import com.mrjy.common.entity.DistributorTeamInfo;
import com.mrjy.common.entity.MessageInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.maintain.dao.IMessageInfoDao;
import com.mrjy.maintain.dao.IUserInfoDao;
import com.mrjy.share.dao.IDistributorInfoDao;
import com.mrjy.share.dao.IDistributorTeamInfoDao;
import com.mrjy.share.dto.DistributorInfoDTO;
import com.mrjy.share.dto.DistributorTeamInfoDTO;

@Service("distributorTeamInfoBo")
public class DistributorTeamInfoBo implements IDistributorTeamInfoBo{
	
	@Resource
	private IDistributorTeamInfoDao distributorTeamInfoDao;
	@Resource
	private IUserInfoDao userInfoDao;
	@Resource
	private IDistributorInfoDao distributorInfoDao;
	@Resource
	private IMessageInfoDao messageInfoDao;

	@Override
	public Object queryListByBelongTeamId(DistributorTeamInfoDTO dto) {
		Map<String,Object> map = new HashMap<String,Object>();
		List<DistributorTeamInfoDTO> list = distributorTeamInfoDao.queryListByBelongTeamId(dto);
		if(null != list && list.size() > 0){
			map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("rows", list);
			map.put("total", list.size());
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "查询成功,无数据");
			map.put("rows", new ArrayList<>());
			map.put("total", 0);
		}
		return map;
	}
	

	@Override
	public Object queryInviteRecord(DistributorTeamInfoDTO dto) {
		Map<String,Object> map = new HashMap<String,Object>();
		List<DistributorTeamInfoDTO> list = distributorTeamInfoDao.queryInviteRecord(dto);
		if(null != list && list.size() > 0){
			map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("rows", list);
			map.put("total", list.size());
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "查询成功,无数据");
			map.put("rows", new ArrayList<>());
			map.put("total", 0);
		}
		return map;
	}


	@Override
	public Object txInvitedByDistributor(DistributorTeamInfoDTO info,HttpSession session) {
		Map<String,Object> map = new HashMap<String,Object>();
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(userInfo == null){
			map.put("err_code", -1);
			map.put("err_msg", "当前没有登录或登录超时");
			return map;
		}
		if(info == null){
			map.put("err_code", -101);
			map.put("err_msg", "新增的信息为空，请检查");
			return map;
		}
		if(info.getDistributorId() == null){//分销商为空
			map.put("err_code", -102);
			map.put("err_msg", "分销商id为空");
			return map;
		}
		if(StringUtils.isBlank(info.getMobilePhone())){
			map.put("err_code", -103);
			map.put("err_msg", "手机号为空");
			return map;
		}
		if(info.getSharingProportion() == null){
			map.put("err_code", -104);
			map.put("err_msg", "分成比例为空");
			return map;
		}
		
//		if(StringUtils.isNotBlank(info.getUserName())){
//			List<UserInfo> userList = userInfoDao.selectByUserName(info.getUserName());
//			if(userList == null){
//				map.put("err_code", 101);
//				map.put("err_msg", "该用户不存在，请确认用户的昵称");
//				return map;
//			}
//		}else {
//			map.put("err_code", -104);
//			map.put("err_msg", "昵称为空");
//			return map;
//		}
		UserInfo ui = userInfoDao.queryByMobilePhone(info.getMobilePhone());
		if(ui == null){
			map.put("err_code", 102);
			map.put("err_msg", "该用户没有绑定手机号");
			return map;
		}else {
//			if(!info.getUserName().equalsIgnoreCase(ui.getUserName())){
//				map.put("err_code", 103);
//				map.put("err_msg", "手机号与昵称不匹配");
//				return map;
//			}
			//检查其是否为分销商
			List<DistributorInfo> dit = distributorInfoDao.isDistributorByUserId(ui.getId());
			if(dit != null && dit.size() > 0){
				map.put("err_code", 104);
				map.put("err_msg", "该用户已是分销商");
				return map;
			}
			//检查是否为分销商队员(后添加讲师，合作方，服务站团队，共用)
			info.setAuditStatus("1");
			info.setUserId(ui.getId());
			List<DistributorTeamInfoDTO>  ditList =  distributorTeamInfoDao.queryByUserId(info);
			if(null != ditList && ditList.size() > 0){
				map.put("err_code", 105);
				map.put("err_msg", "该用户是有团队的人了");
				return map;
			}
		}
		
		//查询推广者的类型
		DistributorInfoDTO dInfo = distributorInfoDao.queryById(info.getDistributorId());
		if(dInfo == null ){
			map.put("err_code", 106);
			map.put("err_msg", "该推广方的信息为空");
			return map;
		}
		DistributorInfoDTO dit = distributorInfoDao.queryByUserId(userInfo.getId());
		//给团队成员设置的自定义分成比例不允许大于其自身的比例
		if(null != dit && info.getSharingProportion() >= dit.getSharingProportion()){
			map.put("err_code", 107);
			map.put("err_msg", "给团队成员设定的分成比例必须小于您的分成比例");
			return map;
		}
		//团队成员不得超过10人
//		int teamNum = distributorTeamInfoDao.queryTeamMemberNumByRecommendId(userInfo.getId());
//		if (teamNum >= 10) {
//			map.put("err_code", 108);
//			map.put("err_msg", "团队成员不能超过10人");
//			return map;
//		}
		
		DistributorTeamInfo insertInfo = new DistributorTeamInfo();
		if("1".equals(dInfo.getDistributorType())){//中级分销商
			insertInfo.setTeamType("01"); 
		}else if ("2".equals(dInfo.getDistributorType())) {//高级分销商
			insertInfo.setTeamType("02");
		}else if ("3".equals(dInfo.getDistributorType())) {//合作方
			insertInfo.setTeamType("3");
		}else if ("51".equals(dInfo.getDistributorType())) {//讲师
			insertInfo.setTeamType("11");
		}else if ("52".equals(dInfo.getDistributorType())) {//明师
			insertInfo.setTeamType("12");
		}else if ("6".equals(dInfo.getDistributorType())) {//服务站
			insertInfo.setTeamType("2");
		}else if ("7".equals(dInfo.getDistributorType())) {//家人团队
			insertInfo.setTeamType("4");
		}
		
		insertInfo.setUserId(ui.getId());
		insertInfo.setRealName(info.getRealName());
		insertInfo.setRemark(info.getRemark());
		insertInfo.setDistributorId(info.getDistributorId());
		//团队成员的分成比例(其上级许诺给其的)
		insertInfo.setSharingProportion(info.getSharingProportion());
		//团队负责人id
		insertInfo.setBelongTeamId(userInfo.getId());
		insertInfo.setDisableFlag(0);
		insertInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
		insertInfo.setAuditStatus("0");
		
		int count = distributorTeamInfoDao.insert(insertInfo);
		
		//发送系统通知
		MessageInfo messageInfo = new MessageInfo();
		messageInfo.setTitle("邀请团队成员");
		messageInfo.setContent("365家长课堂推广大使 "+dit.getDistributorName()+" 邀请你加入ta的推广团队，ta赋予你高达"+info.getSharingProportion()+"的一级推广分成，赶快来加入吧！");
		messageInfo.setSendTime(new Date());
		messageInfo.setSenderId(1);
		messageInfo.setUserId(ui.getId());
		messageInfo.setReadTag(0);
		messageInfo.setMsgModule(8);
		messageInfoDao.insert(messageInfo);
		
		if(count == 1){
			map.put("err_code", 0);
			map.put("err_msg", "插入成功");
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "插入失败");
		}
		return map;
	}
	

	@Override
	public Object addByDistributor(DistributorTeamInfoDTO info) {
		Map<String,Object> map = new HashMap<String,Object>();
		
		DistributorTeamInfo diTeamInfo = distributorTeamInfoDao.queryInfoByUserId(info);
		if(null != diTeamInfo){
			map.put("err_code", 1);
			map.put("err_msg", "其他团队成员，不能添加");
			if(diTeamInfo.getBelongTeamId() == info.getDistributorId()){
				map.put("err_code", 1);
				map.put("err_msg", "已经是你团队成员");
			}
			return map;
		}
		
		DistributorInfoDTO dit = distributorInfoDao.queryByUserId(info.getDistributorId());
		//给团队成员设置的自定义分成比例不允许大于其自身的比例
		if(null != dit && info.getSharingProportion() >= dit.getSharingProportion()){
			map.put("err_code", 107);
			map.put("err_msg", "成员比例不能大于" + dit.getSharingProportion() * 100);
			return map;
		}
		
		DistributorTeamInfo insertInfo = new DistributorTeamInfo();
		if("1".equals(dit.getDistributorType())){//中级分销商
			insertInfo.setTeamType("01"); 
		}else if ("2".equals(dit.getDistributorType())) {//高级分销商
			insertInfo.setTeamType("02");
		}else if ("3".equals(dit.getDistributorType())) {//合作方
			insertInfo.setTeamType("3");
		}else if ("51".equals(dit.getDistributorType())) {//讲师
			insertInfo.setTeamType("11");
		}else if ("52".equals(dit.getDistributorType())) {//明师
			insertInfo.setTeamType("12");
		}else if ("6".equals(dit.getDistributorType())) {//服务站
			insertInfo.setTeamType("2");
		}else if ("7".equals(dit.getDistributorType())) {//家人团队
			insertInfo.setTeamType("4");
		}
		
		insertInfo.setUserId(info.getUserId());
		insertInfo.setRealName(info.getUserName());
		insertInfo.setRemark(info.getRemark());
		insertInfo.setDistributorId(info.getDistributorId());
		//团队成员的分成比例(其上级许诺给其的)
		insertInfo.setSharingProportion(info.getSharingProportion());
		//团队负责人id
		insertInfo.setBelongTeamId(info.getDistributorId());
		insertInfo.setDisableFlag(0);
		insertInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
		insertInfo.setAuditStatus("1");
		
		int count = distributorTeamInfoDao.insert(insertInfo);
		
		//发送系统通知
		MessageInfo messageInfo = new MessageInfo();
		messageInfo.setTitle("邀请团队成员");
		messageInfo.setContent("365家长课堂推广大使 "+dit.getDistributorName()+" 邀请你加入ta的推广团队，ta赋予你高达"+info.getSharingProportion()+"的一级推广分成，赶快来加入吧！");
		messageInfo.setSendTime(new Date());
		messageInfo.setSenderId(1);
		messageInfo.setUserId(info.getUserId());
		messageInfo.setReadTag(0);
		messageInfo.setMsgModule(8);
		messageInfoDao.insert(messageInfo);
		
		if(count == 1){
			map.put("err_code", 0);
			map.put("err_msg", "插入成功");
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "插入失败");
		}
		return map;
	}


	@Override
	public Object isInvitedByUserId(DistributorTeamInfoDTO info,HttpSession session) {
		Map<String,Object> map = new HashMap<String,Object>();
		DistributorTeamInfoDTO paramDTO = new DistributorTeamInfoDTO();
		paramDTO.setUserId(info.getUserId());
		paramDTO.setAuditStatus("1");//接受邀请的状态
		List<DistributorTeamInfoDTO> dtoList = distributorTeamInfoDao.queryByUserId(paramDTO);
		if(dtoList != null && dtoList.size() > 0){//还没接受邀请
			map.put("err_code", 0);
			map.put("err_msg", "已经接受邀请");
			map.put("rows", null);
		}else {
			paramDTO.setAuditStatus("0");//邀请中
			List<DistributorTeamInfoDTO> list = distributorTeamInfoDao.queryByUserId(paramDTO);
			if(list != null && list.size() > 0){
				map.put("err_code", 1);
				map.put("err_msg", "还没接受邀请");
				map.put("rows", list);
			}else {
				map.put("err_code", 2);
				map.put("err_msg", "没有被邀请中");
				map.put("rows", list);
			}
		}
		return map;
	}


	@Override
	public Object txOptInvited(DistributorTeamInfoDTO info,HttpSession session) {
		Map<String,Object> map = new HashMap<String,Object>();
		if(info == null){
			map.put("err_code", -101);
			map.put("err_msg", "更新的对象不能为空");
			return map;
		}
		if(info.getUserId() == null){
			map.put("err_code", -102);
			map.put("err_msg", "用户id不能为空");
			return map;
		}
		if(info.getId() == null){
			map.put("err_code", -102);
			map.put("err_msg", "id不能为空");
			return map;
		}
		if(StringUtils.isBlank(info.getAuditStatus())){
			map.put("err_code", -103);
			map.put("err_msg", "审核状态不能为空");
			return map;
		}
		
		if("1".equals(info.getAuditStatus())){
			info.setBindTime(new Timestamp(System.currentTimeMillis()));
		}
		int count  = distributorTeamInfoDao.updateById(info);
		if(count == 1){
			if("1".equals(info.getAuditStatus())){//接受邀请
				info.setAuditStatus("2");
				distributorTeamInfoDao.updateAuditStatus(info);//更新其他数据邀请中的数据为拒绝状态
			}
			
			//接收邀请,插入一条团队成员推广身份, 分成比例为自定义比例
			UserInfo userInfo = userInfoDao.queryByIdForBack(info.getUserId());
			DistributorTeamInfo teamInfo = distributorTeamInfoDao.selectByPrimaryKey(info.getId());
			if (null!=teamInfo) {
				DistributorInfo distributorInfo = new DistributorInfo();
				distributorInfo.setUserId(info.getUserId());
				distributorInfo.setCreateTime(new Timestamp(System.currentTimeMillis()));
				distributorInfo.setDistributorType("team");
				distributorInfo.setDisabledFlag(0);
				distributorInfo.setSharingProportion(teamInfo.getSharingProportion());
				distributorInfo.setShareCode(userInfo==null?null:userInfo.getRecommendCode());
				distributorInfo.setDistributorName(teamInfo.getRealName());
				distributorInfoDao.insert(distributorInfo);
			}
			
			map.put("err_code", 0);
			map.put("err_msg", "更新成功");
		}else{
			map.put("err_code", 1);
			map.put("err_msg", "更新失败");
		}
		
		return map;
	}


	@Override
	public Object isCaninvited(DistributorTeamInfoDTO info) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("err_code", 0);
		map.put("err_msg", "可以被邀请");
		if(StringUtils.isBlank(info.getMobilePhone())){
			map.put("err_code", -103);
			map.put("err_msg", "手机号为空");
			return map;
		}
		UserInfo ui = userInfoDao.queryByMobilePhone(info.getMobilePhone());
		if(ui == null){
			map.put("err_code", 102);
			map.put("err_msg", "该用户没有绑定手机号");
			return map;
		}else {
//			if(!info.getUserName().equalsIgnoreCase(ui.getUserName())){
//				map.put("err_code", 103);
//				map.put("err_msg", "手机号与昵称不匹配");
//				return map;
//			}
			//检查其是否为分销商
			List<DistributorInfo> di = distributorInfoDao.isDistributorByUserId(ui.getId());
			if(di != null && di.size() > 0){
				map.put("err_code", 104);
				map.put("err_msg", "该用户已是分销商");
				return map;
			}
			//检查是否为分销商队员
			info.setAuditStatus("1");
			info.setUserId(ui.getId());
			List<DistributorTeamInfoDTO>  ditList =  distributorTeamInfoDao.queryByUserId(info);
			if(null != ditList && ditList.size() > 0){
				map.put("err_code", 105);
				map.put("err_msg", "该用户已是分销商队员");
				return map;
			}
		}
		
		return map;
	}


	@Override
	public Object isCanInvite(DistributorTeamInfo info) {
		Map<String,Object> map = new HashMap<String,Object>();
		int row = distributorTeamInfoDao.isCanInvite(info);
		if (row >= 10) {
			map.put("err_code", 100);
			map.put("err_msg", "邀请已达上限");
			return map;
		}
		map.put("err_code", 0);
		map.put("err_msg", "可以邀请");
		return map;
	}



}


		
	